/*
 * @(#)Main.java
 *
 */

package diablo3;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import diablo3.mail.MailSenderInfo;
import diablo3.mail.SimpleMailSender;
import diablo3.process.Computer;
import diablo3.process.Processer;
import diablo3.process.RestartD3;
import diablo3.support.Util;

/**
 * description
 * @author  huangyixiang
 * @version 2012-8-27
 */
public class Main {
	
	public static boolean RUNNING = true;
	public static long SCREEN_SHOT_INTERVAL = 15;
	
	private static String ip = null;
	private static final Log LOG = LogFactory.getLog(Main.class);
	
	
	
	public static void main(String[] args) throws ClassNotFoundException {
		Class.forName("diablo3.Config");
		
		if(Config.Main.sendIpMail){
			sendIPMail();
		}
		if(Config.Main.systemPowerOff){
			setSystemPowerOffTime(args);					
		}
		if(Config.Main.sendScreenShotMail){
			sendScreenShotMailLoop();
		}
		RestartD3 restartD3 = new RestartD3();
		if(Config.Main.startD3AfterBoot){
			restartD3.startD3All();
		}
		if(Config.Main.autoRestartD3){
			restartD3.autoRestartD3();
		}
		if(Config.Main.restartAMFCycle){
			restartD3.reestartAMFCycle();
		}
		if(Config.Main.connectAMFCycle){
			KeepAMFConnect amfConnect = new KeepAMFConnect();
			amfConnect.connectLoop();
		}
	}
	
	public static void setSystemPowerOffTime(String[] hour){
		if(hour == null || hour.length == 0 || hour[0] == null){
			return;
		}
		float fHour = 0;
		try{
			fHour = Float.valueOf(hour[0].toString());
		}
		catch (NumberFormatException e) {
			LOG.info("错误的关机时间:" + hour[0].toString());
			return;
		}
		if(fHour <= 0){
			LOG.info("不执行关机:" + hour[0].toString());
			return;
		}
		Computer computer = new Computer();
		String time = computer.setSystemPowerOffTime(fHour);
		sendPowerOffTimeMail(time);
	}
	
	public static void sendPowerOffTimeMail(String time){
		String subject = "预计将在 " + time + " 关机";
		String content = "";
		sendMyMail(subject, content);
	}
	
	public static void sendScreenShotMailLoop(){
		LOG.info("启动循环发送屏幕截图程序");
		new Thread(){
			@Override
			public void run() {
				while(RUNNING){
					try {
						Thread.sleep(SCREEN_SHOT_INTERVAL * 60 * 1000);
					} catch (InterruptedException e) {
						LOG.error(e.getMessage(),e);
					}
					sendScreenShotMail();
				}
			}
		}.start();
	}
	
	public static boolean sendScreenShotMail(){

        String a2 = Util.formatDate2String(new Date());
        String ip = Main.ip;
        if(ip == null || ip.trim().equals("")){
        	ip = IP.getOutIPAddress()[0];
        }
		String subject = a2 + " 截屏    " + ip;
		String content = "";
		Snapshot snapshot = new Snapshot();
		
		String fullScreenfileNamePath = snapshot.screenShot();
		sendMyMail(subject, content, fullScreenfileNamePath);
		snapshot.deleteScreenShot(fullScreenfileNamePath);
		
//		String gameScreenfileNamePath = snapshot.snapshot(properties.getProperty("Snapshot.snapshotKey"), properties.getProperty("Snapshot.gameSnapshotPath"));
//		sendMyMail(subject, content, fullScreenfileNamePath , gameScreenfileNamePath);
//		snapshot.deleteScreenShot(gameScreenfileNamePath);
		return true;
	
		
	}
	
	public static boolean sendIPMail(){
		String[] ip = IP.getOutIPAddress();
		if(Util.isBlank(ip[0])){
			return false;
		}
		Main.ip = ip[0];
		boolean ret =  sendMyMail("本次启动IP地址为：" + ip[0], ip[1]);
		if(!ret){
			LOG.info("发送IP邮件失败");
		}
		return ret;
	}
	
	public static boolean sendMyMail(String subject , String content , String... attachFileNames){
		// 这个类主要是设置邮件
		MailSenderInfo mailInfo = new MailSenderInfo();
		mailInfo.setMailServerHost("smtp.163.com");
		mailInfo.setMailServerPort("25");
		mailInfo.setValidate(true);
		mailInfo.setUserName("huangjin43385607@163.com");
		mailInfo.setPassword("woaishe");
		mailInfo.setFromAddress("huangjin43385607@163.com");
		mailInfo.setToAddress("huangjin43385607@163.com");
		mailInfo.setSubject(subject);
		mailInfo.setContent(content);
		mailInfo.setAttachFileNames(attachFileNames);
		// 这个类主要来发送邮件
		SimpleMailSender sms = new SimpleMailSender();
		return sms.sendHtmlMail(mailInfo);// 发送html格式
	}
}
